package project.euler.honk.problem.to50;

import java.math.BigInteger;
import java.util.Arrays;

final class Problem27 {

	private static final int[] primes = new int[1000];

	public static void main(String[] args) {
		long start = System.currentTimeMillis();

		BigInteger x = BigInteger.ONE;
		for (int i = 0; i < 1000; i++) {
			x = x.nextProbablePrime();
			primes[i] = x.intValue();
		}

		int maxa = 0;
		int maxb = 0;
		int maxNumber = 0;
		for (int a = -999; a < 1000; a++) {
			for (int b = -999; b < 1000; b++) {
				int count = countPrimes(a, b);
				if (count > maxNumber) {
					maxa = a;
					maxb = b;
					maxNumber = count;
				}
			}
		}
		long end = System.currentTimeMillis();
		System.out.println("Result: " + (maxa * maxb) + " Time: " + (end - start));
	}

	private static int countPrimes(int a, int b) {
		for (int i = 0;; i++) {
			int value = i * i + a * i + b;
			int pos = Arrays.binarySearch(primes, value);
			if (pos >= 1000 || pos < 0 || primes[pos] != value)
				return i;
		}
	}

}
